<template>
  <div>
    <div class='msg'>
      <div class='card'>
        <span class='card-o'></span>
        <span class='card-t'>修改密码</span>
      </div>
    </div>
    <div class='passWord'>
      <a-form :form='form'>
        <a-form-item
          :label-col='formItemLayout.labelCol'
          :wrapper-col='formItemLayout.wrapperCol'
          label='旧密码'
          name='sd'
        >
          <a-input
            type='password'
            v-decorator="[
          'oldPassword',
            {
              rules: [{ required: true, message: '请输入密码' }, { min: 4,  message: '密码可输入长度未4-10位', max: 10 }],
              validateTrigger: 'blur',
            }
          // { rules: [{ required: true, min:3,max:9,message: 'Please input your name' }] },
        ]"
            placeholder='请输入原密码'
          />
        </a-form-item>
        <a-form-item
          :label-col='formItemLayout.labelCol'
          :wrapper-col='formItemLayout.wrapperCol'
          label='新密码'
          name='sd1'
        >
          <a-input
            type='password'
            v-decorator="[
          'newPassword',
            {
              rules: [{ required: true, message: '请输入密码' }, { min: 4,  message: '密码可输入长度未4-10位', max: 10 }],
              validateTrigger: 'blur',
            }
          // { rules: [{ required: true,min:3,max:9, message: 'Please input your nickname',pattern:/^\S*(?=\S{6,})(?=\S*\d)(?=\S*[A-Z])(?=\S*[a-z])(?=\S*[!@#$%^&*? ])\S*$/}] },
        ]"
            placeholder='请输入新密码'
          />
        </a-form-item>
        <a-form-item
          :label-col='formItemLayout.labelCol'
          :wrapper-col='formItemLayout.wrapperCol'
          label='确认密码'
          name='sd3'
          help='请再次输入新密码'
        >
          <a-input
            type='password'
            v-decorator="[
          'again',
            {
              rules: [{ required: true, message: '请输入密码' }, { min: 4,  message: '密码可输入长度未4-10位', max: 10 }],
              validateTrigger: 'blur',
            }
          // { rules: [{ required: true, message: 'Please input your nickname' }] },
        ]"
            placeholder='请再次输入新密码'
          />
        </a-form-item>

        <a-form-item :label-col='formTailLayout.labelCol' :wrapper-col='formTailLayout.wrapperCol'>
          <a-button type='primary' @click='check'>
            提交
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script>
const formItemLayout = {
  labelCol: { span: 2 },
  wrapperCol: { span: 8 }
}
const formTailLayout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 8, offset: 4 }
}

import { changePassword } from '@/api/user'

export default {
  name: 'changePass',
  components: {},
  data() {
    return {
      formItemLayout,
      formTailLayout,
      form: this.$form.createForm(this, { name: 'dynamic_rule' })
    }
  },
  methods: {
    check() {
      let msgs = this.form.getFieldsValue()
      this.form.validateFields((err, msgs) => {
        if (!err && msgs.again === msgs.newPassword) {
          msgs.id = this.$store.state.user.info.id
          changePassword(msgs).then(res => {
            this.$message.success('密码更新成功!')
            setTimeout(() => {
              this.$store.dispatch('Logout').then(() => {
                this.$router.push({ name: 'login' })
              })
            }, 1000)
          })
        } else {
          this.$message.error('两次输入不一致，请重新输入')
          // this.form.resetFields()
        }
      })
    }
  }
}
</script>

<style lang='less' scoped>
.msg {
  width: auto;
  height: auto;

  .card {
    .card-o {
      display: inline-block;
      width: 3px;
      height: 16px;
      background: #4470D8;
      border-radius: 0px 0px 0px 0px;
      margin-right: 10px;
      opacity: 1;
    }

    .card-t {
      font-size: 18px;
      font-family: Microsoft YaHei, Microsoft YaHei;
      font-weight: 400;
      color: #3D4149;
      line-height: 24px;
      -webkit-background-clip: text;
    }
  }
}

.passWord {
  width: auto;
  height: auto;
}
</style>